package org.client.iot.util;

import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 权限申请获取类
 */
public class PermissionManager {

    private static final String TAG = "PermissionManager";

    /**
     * android 6.0 以上需要动态申请权限
     */
    public static void initPermission(Activity activity) {
        String[] permissions = getPermission();
        new RxPermissions(activity)
                .requestEach(permissions)
                .subscribe(new Observer<Permission>() {
                    private Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Permission permission) {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && permission.name.equals(Manifest.permission.FOREGROUND_SERVICE)) {
                            return;
                        }
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
                            String permissionName = permission.name;
                            if (permissionName.equals(Manifest.permission.BLUETOOTH_SCAN) || permissionName.equals(Manifest.permission.BLUETOOTH_CONNECT)) {
                                return;
                            }
                        }
                        if (permission.granted) {
                            Log.d(TAG, "用户同意了该权限请求:" + permission.name);
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            Toast.makeText(activity, "权限" + permission.name + "被拒绝,下次还会提示请求对话框", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(activity, "权限" + permission.name + "被拒绝,请自行到系统设置中开启相关权限", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }

    public static void checkPermission(Activity activity, AcceptPermissionListener acceptPermissionListener, String... permission) {
        new RxPermissions(activity)
                .request(permission).subscribe(new Observer<Boolean>() {
                    private Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Boolean isAgree) {
                        if (isAgree) {
                            acceptPermissionListener.onSuccess();
                        } else {
                            acceptPermissionListener.onFail();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        acceptPermissionListener.onError(e);
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }


    public static String[] getPermission() {
//        return new String[]{
//                Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                Manifest.permission.ACCESS_NETWORK_STATE,
//                Manifest.permission.INTERNET,
//                Manifest.permission.READ_EXTERNAL_STORAGE,
//                Manifest.permission.CAMERA,
//        };
        return new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.USE_SIP,

        };
    }

    public static String[] getLOCATIONPermission() {
        return new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,

        };
    }

        public static String[] getBLEPermission () {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                return new String[]{Manifest.permission.BLUETOOTH_ADMIN,
                        Manifest.permission.BLUETOOTH,
                        Manifest.permission.BLUETOOTH_SCAN,
                        Manifest.permission.BLUETOOTH_CONNECT,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.ACCESS_WIFI_STATE,
                        Manifest.permission.CHANGE_WIFI_STATE};
            } else {
                return new String[]{Manifest.permission.BLUETOOTH_ADMIN,
                        Manifest.permission.BLUETOOTH,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.ACCESS_WIFI_STATE,
                        Manifest.permission.CHANGE_WIFI_STATE};
            }
        }

        public interface AcceptPermissionListener {
            void onSuccess();

            void onFail();

            void onError(Throwable e);
        }


    }
